/**
 * Writed by yura and mak
 */

package wz.legend.util;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import wz.legend.Context;
import wz.legend.HttpContext;
import wz.legend.Status;
import wz.legend.handlers.farming.FarmingHandler.DelayStartTask;
import wz.legend.handlers.info.InfoConstants;


import java.io.IOException;
import java.io.StringReader;
import java.net.URLDecoder;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.awt.*;

public class Utils {
    private static Logger LOG = Logger.getLogger(Utils.class);

    public static String startProcess(String command) throws Exception {
        LOG.debug("start process command " + command);
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();
        String answer = IOUtils.toString(process.getInputStream());
        LOG.debug("process answer " + answer);
        return answer;
    }

    public static boolean startProcessAsync(String command) throws Exception {
        LOG.debug("start process command " + command);
        Runtime.getRuntime().exec(command);
        return true;
    }

    public static String consoleString(String str) throws Exception {
        return new String(str.getBytes("Cp866"), "Cp1251");
    }

    public static String getAttributeValue(Node node, String name) {
        return node.getAttributes().getNamedItem(name).getNodeValue();
    }

    public static int getAttributeInt(Node node, String name) {
        return Integer.parseInt(node.getAttributes().getNamedItem(name)
                .getNodeValue());
    }

    public static Point getItemPosition(Node node) {
        int x = Utils.getAttributeInt(node, "x");
        int y = Utils.getAttributeInt(node, "y");
        return new Point(x, y);
    }

    public static List<Node> convertNodeList(NodeList nodeList) {
        List<Node> list = new ArrayList<Node>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            list.add(nodeList.item(i));
        }
        return list;
    }

    public static int getRandomInterval(int min, int max) {
        if (max > 0 && min >= 0 && max > min) {
            return min + new Random().nextInt(max - min);
        }
        return 0;
    }

    public static String getResponseContent(HttpContext httpContext)
            throws IOException {
        return IOUtils.toString(httpContext.getResponse().getEntity()
                .getContent());
    }

    @SuppressWarnings("unchecked")
    public static int getCrossIndex(Context context, String name) {
        List<String> list = (List<String>) context
                .getProperty(InfoConstants.AREA_CROSS);
        if (list == null) {
            context.setStatus(Status.INACTIVE);
            throw new RuntimeException("Cross info not found");
        }

        int index = list.indexOf(name);
        if (index < 0) {
            context.setStatus(Status.INACTIVE);
            throw new RuntimeException("Cross info not found - wrong index");
        }

        return index;
    }
}
